<?php

class IzinController extends Controller
{
    public $layout='//layouts/column2';
    
    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'perusahaan_id',
		'value' => '($data->perusahaan) ? $data->perusahaan->nama_perusahaan : "-"',
                ),array(
                    'name' => 'jenis_izin',
                    'value' => '($data->jenis_izin) ? $data->perusahaan->jenis_usaha." ".$data->jenis_izin: "-"',
                ),array(
                        'name' => 'lokasi',
                        'value' => '($data->lokasi && $data->provinsi) ? $data->lokasi.", ".$data->provinsi->nama : $data->kabupaten->nama.", ".$data->provinsi->nama',
                ),array(
                        'name' => 'kapasitas_pengolahan',
                        'filter'=>false,
                        'value' => '($data->kapasitas_pengolahan) ? $data->kapasitas_pengolahan." ".$data->satuan->nama : "-"',
                ),array(
                        'name' => 'status_izin',
                        'filter' => CHtml::dropDownList(get_class($model) . '[status_izin]', $model->status_izin, array('Masih Berlaku'=>'Masih Berlaku','Dalam Proses'=>'Dalam Proses','Kadaluarsa'=>'Kadaluarsa',''=>'Pilih Status..'), array('class' => 'form-control')),
                        'value' => '$data->status_izin',
                ),array(
                        'name' => 'tanggal_berakhir',
                        'filter'=>false,
                        'value' => '($data->tanggal_berakhir && $data->tanggal_berakhir!=="0000-00-00") ? $data->tanggal_berakhir : "-"',
                ),array(
                //'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn',
                'template'=>'{view}',
                'buttons'=>array(
                    'view'=>array(
                         'url'=>'array("izin/read","id"=>$data->id)',
                    ),
                ),
            ),
            );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'perusahaan_id',
	'value' => ($model->Perusahaan) ? $model->Perusahaan->id : "-",
),array(
	'name' => 'nomor_sk',
	'value' => ($model->nomor_sk) ? $model->nomor_sk : "-",
),array(
	'name' => 'tentang',
	'value' => ($model->tentang) ? $model->tentang : "-",
),array(
	'name' => 'tanggal_terbit',
	'value' => ($model->tanggal_terbit) ? $model->tanggal_terbit : "-",
),array(
	'name' => 'tanggal_berakhir',
	'value' => ($model->tanggal_berakhir) ? $model->tanggal_berakhir : "-",
),array(
	'name' => 'penanggung_jawab',
	'value' => ($model->penanggung_jawab) ? $model->penanggung_jawab : "-",
),array(
	'name' => 'kode_izin_usaha',
	'value' => ($model->kode_izin_usaha) ? $model->kode_izin_usaha : "-",
),array(
	'name' => 'kapasitas_pengolahan',
	'value' => ($model->kapasitas_pengolahan) ? $model->kapasitas_pengolahan : "-",
),array(
	'name' => 'lokasi',
	'value' => ($model->lokasi) ? $model->lokasi : "-",
),array(
	'name' => 'provinsi_id',
	'value' => ($model->Provinsi) ? $model->Provinsi->nama : "-",
),array(
	'name' => 'status_izin',
	'value' => ($model->status_izin) ? $model->status_izin : "-",
),array(
	'name' => 'upload_dokumen',
	'value' => ($model->upload_dokumen) ? $model->upload_dokumen : "-",
),array(
	'name' => 'created',
	'value' => ($model->created) ? $model->created : "-",
),array(
	'name' => 'modified',
	'value' => ($model->modified) ? $model->modified : "-",
),        );
        return $viewList;
    }

    public function actionIndex($id=NULL)
    {
        switch ($id) {
            case 1:
                $jenisUsaha = 'Pengolahan MB';
                $jenisUsahaAlias = 'Pengolahan Minyak Bumi';
                break;
            case 2:
                $jenisUsaha = 'Pengolahan GB';
                $jenisUsahaAlias = 'Pengolahan Gas Bumi';
                break;
            case 3:
                $jenisUsaha = 'Pengolahan HO';
                $jenisUsahaAlias = 'Pengolahan Hasil Olahan';
                break;
            default:
                $jenisUsaha = '';
                $jenisUsahaAlias = 'Semua Data Perusahaan';
                break;
        }
//        if(Yii::app()->user->isOperatorMB())
//        {
//            $jenisUsaha = 'Pengolahan MB';
//            $jenisUsahaAlias = 'Pengolahan Minyak Bumi';
//        }
//        elseif(Yii::app()->user->isOperatorGB())
//        {
//                $jenisUsaha = 'Pengolahan GB';
//                $jenisUsahaAlias = 'Pengolahan Gas Bumi';   
//        }
//        elseif(Yii::app()->user->isOperatorHO())
//        {
//                $jenisUsaha = 'Pengolahan HO';
//                $jenisUsahaAlias = 'Pengolahan Hasil Olahan';
//        }
//        else {
//                $jenisUsaha = '';
//                $jenisUsahaAlias = 'Semua Data Perizinan';
//        }
        $model = new Izin('search');
        $model->unsetAttributes();
        if (isset($_GET['Izin']))
            $model->attributes = $_GET['Izin'];
        if(!empty($id))
           $model->jenis_usaha = $jenisUsaha;
        $this->render('index',array(
            'model'=>$model,
            'jenisUsahaAlias'=>$jenisUsahaAlias,
        ));
    }

    public function actionRead($id) {
        $model = $this->loadModel($id);
        $this->render('read', array(
            'model' => $model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionOperator($id=null)
    {
        switch ($id) {
            case 1:
                $jenisUsaha = 'Pengolahan MB';
                $jenisUsahaAlias = 'Pengolahan Minyak Bumi';
                break;
            case 2:
                $jenisUsaha = 'Pengolahan GB';
                $jenisUsahaAlias = 'Pengolahan Gas Bumi';
                break;
            case 3:
                $jenisUsaha = 'Pengolahan HO';
                $jenisUsahaAlias = 'Pengolahan Hasil Olahan';
                break;
            default:
                $jenisUsaha = '';
                $jenisUsahaAlias = 'Semua Data Perusahaan';
                break;
        }
        $model = new Izin('search');
        $model->unsetAttributes();
        if (isset($_GET['Izin']))
            $model->attributes = $_GET['Izin'];
        if(!empty($id))
           $model->jenis_usaha = $jenisUsaha;
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin($id=null)
    {
        switch ($id) {
            case 1:
                $jenisUsaha = 'Pengolahan MB';
                $jenisUsahaAlias = 'Pengolahan Minyak Bumi';
                break;
            case 2:
                $jenisUsaha = 'Pengolahan GB';
                $jenisUsahaAlias = 'Pengolahan Gas Bumi';
                break;
            case 3:
                $jenisUsaha = 'Pengolahan HO';
                $jenisUsahaAlias = 'Pengolahan Hasil Olahan';
                break;
            default:
                $jenisUsaha = '';
                $jenisUsahaAlias = 'Semua Data Perusahaan';
                break;
        }
        $model = new Izin('search');
        $model->unsetAttributes();
        if (isset($_GET['Izin']))
            $model->attributes = $_GET['Izin'];
        if(!empty($id))
           $model->jenis_usaha = $jenisUsaha;
        $this->render('admin',array(
            'model'=>$model,
        ));
    }
    
    public function actionExport() {
        if(isset($_POST['Izin']))
        {
//            $jenisUsaha = $_POST['Izin']['jenis_usaha'];
//            $statusIzin = $_POST['Izin']['status_izin'];
//            $c = new CDbCriteria;
//            $c->with = array(
//                'Perusahaan'=>array(
//                    'select'=>'nama_perusahaan,jenis_usaha'
//                ),
//            );
//            if(!empty($jenisUsaha))
//            {
//                $c->addCondition('Perusahaan.jenis_usaha='.$jenisUsaha);
//            }
//            if(!empty($jenisIzin))
//            {
//                $c->addCondition('t.jenis_izin='.$jenisUsaha);
//            }
            $model = Izin::model()->findAll();
            print_r($model->perusahaan->nama_perusahaan);
        }
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $checklistAdmin=new CActiveDataProvider('ChecklistAdmin', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,
                ),
            'pagination'=>false,
            )
        );
        $checklistTeknis=new CActiveDataProvider('ChecklistTeknis', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
        );
        $checklistIzin=new CActiveDataProvider('ChecklistPerizinan', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
        );
        $checklistSurat=new CActiveDataProvider('ChecklistSurat', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
        );
        $checklistEvaluasi=new CActiveDataProvider('ChecklistEvaluasi', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
        );
        $checklistLain=new CActiveDataProvider('ChecklistLain', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
        );
        $this->render('view',array(
            'checklistAdmin'=>$checklistAdmin,
            'checklistTeknis'=>$checklistTeknis,
            'checklistIzin'=>$checklistIzin,
            'checklistSurat'=>$checklistSurat,
            'checklistEvaluasi'=>$checklistEvaluasi,
            'checklistLain'=>$checklistLain,
        ));
    }

    public function actionCreate()
    {
        $model=new Izin;
        $this->performAjaxValidation($model);
        if(isset($_POST['Izin']))
        {
                $model->attributes=$_POST['Izin'];
                $id = $model->perusahaan_id;
                $perusahaan = Perusahaan::model()->findByPk($id);
                $badanUsaha = $perusahaan->bentuk_badan_usaha;
                $nama = $perusahaan->nama_perusahaan;
                
                if($perusahaan->jenis_usaha == 'Pengolahan MB')
                    $dirName = '1 Pengolahan MB';
                if($perusahaan->jenis_usaha == 'Pengolahan GB')
                    $dirName = '2 Pengolahan GB';
                if($perusahaan->jenis_usaha == 'Pengolahan HO')
                    $dirName = '3 Pengolahan HO';
                
                $fileName = $id.'-'.$badanUsaha.' '.$nama.'.pdf';
                if(!empty($model->tanggal_terbit))
                {
                    $tanggalTerbit = str_replace('-', '', $model->tanggal_terbit);
                    $fileName = $id.'-'.$tanggalTerbit.'-'.$badanUsaha.' '.$nama.'.pdf';
                }
            $file = CUploadedFile::getInstance($model, 'upload_dokumen');
            if (is_object($file) && get_class($file) === 'CUploadedFile')
                $model->upload_dokumen = $file;
            if ($model->validate()) {
                $rootDir = '/uploaded_files/attachment/'.$dirName.'/'.$id.' '.$badanUsaha.' '.$nama;
                $path = YiiBase::getPathOfAlias('webroot').$rootDir;
                if (is_object($model->upload_dokumen)) {
                    $model->upload_dokumen->saveAs($path.'/3 Izin/'.$fileName);
                }
                $model->upload_dokumen = $fileName;
                $model->path_dokumen = $rootDir;
                if ($model->save())
                {
                    //Change status to 'Ya' on ChecklistPerizinan table
                    $checklist = ChecklistPerizinan::model()->findByAttributes(array('perusahaan_id'=>$id));
                    $checklist->status_kelengkapan = 'Ada';
                    $checklist->upload_dokumen = $fileName;
                    $checklist->save();
                    $this->redirect(array('izin/admin'));
                }
            }
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }
    
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['Izin']))
        {
            $model->attributes=$_POST['Izin'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function loadModel($id)
    {
        $model=Izin::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='izin-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
